package com.lizardmind.haofudoctor.ui.aboutMe

import android.app.ProgressDialog
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.view.View
import com.blankj.utilcode.util.TimeUtils
import com.google.gson.Gson
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.linxiao.framework.activity.BaseActivity
import com.linxiao.framework.adapter.BaseAdapter
import com.linxiao.framework.adapter.DividerGridItemDecoration
import com.linxiao.framework.util.color
import com.linxiao.framework.util.fromJson
import com.linxiao.framework.util.getDataBinding
import com.linxiao.framework.util.ke
import com.lizardmind.haofudoctor.R
import com.lizardmind.haofudoctor.databinding.ActivityMyPublishBinding
import com.lizardmind.haofudoctor.databinding.ItemMyPublishBinding
import com.lizardmind.haofudoctor.manager.postApi
import com.lizardmind.haofudoctor.other.DoctorInfo
import com.lizardmind.haofudoctor.other.MyPublishBean
import com.lizardmind.haofudoctor.other.Request
import io.reactivex.functions.Action
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.activity_my_publish.*
import kotlinx.android.synthetic.main.include_title_text.*

//我的发布activity
class MyPublishActivity : BaseActivity() {

    private val list= arrayListOf<MyPublishBean>()
    private var page=1
    private val count=10

    val adapter by lazy { BaseAdapter<MyPublishBean,ItemMyPublishBinding>(R.layout.item_my_publish,list) }

    val binding by lazy { getDataBinding<ActivityMyPublishBinding>(R.layout.activity_my_publish) }
    override fun onCreatRootView(): View {
        return binding.root
    }

    override fun onInitView(savedInstanceState: Bundle?) {
        title.apply {
            titleText.text="我的发布"
            imgLeft.setImageResource(R.mipmap.back)
            imgLeft.setOnClickListener{finish()}
        }

        rvMyPublish.layoutManager=LinearLayoutManager(this)
        rvMyPublish.adapter=adapter
//        rvMyPublish.addItemDecoration(DividerGridItemDecoration(this,3,this.color(R.color.windowBackground)))

        getMyPublish()
//        add()

        refreshMyPublish.setOnRefreshListener(object : RefreshListenerAdapter(){
            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                page=1
                list.clear()
                getMyPublish(1)
            }
            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                page++
                getMyPublish(2)
            }
        })



    }

    //加载我的发布信息 0=自启动刷新，1=刷新，2=加载
    private fun getMyPublish(flag: Int =0){
//        val dialog=ProgressDialog(this)
//        dialog.show()
        if (flag==0)
            refreshMyPublish.startRefresh()
        postApi<Request<List<MyPublishBean>>>(hashMapOf("action" to "get_my_release",
                "token" to DoctorInfo.token,
                "p" to page.toString(),
                "count" to count.toString()),
                Consumer {
                    it.dispose(notData = {
                        },data = {
                        list.addAll(it)
                        adapter.notifyDataSetChanged()
                    })
                }, Action {
//                dialog.dismiss()
                if (flag==1 || flag==0){
                    refreshMyPublish.finishRefreshing()
                }else if (flag==2){
                    refreshMyPublish.finishLoadmore()
                }
        })
    }

    fun add(){
        postApi<Request<String>>(hashMapOf("action" to "add_my_release",
                "token" to DoctorInfo.token,
                "title" to "标题2",
                "content" to "看见看见了个空间还可根据黑龙江和科技股份和看风景花开了个飞轮海考虑各方看"),
                Consumer {
                    it.dispose(notData = {

                    },
                            data = {

                            })
                }, Action {

        })
    }

}
